package com.screen.screendisplay.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.dryport.DryportOpermonLocomotive;
import com.screen.screendisplay.mapper.dryport.DryportOpermonLocomotiveMapper;
import com.screen.screendisplay.service.dryport.DryportOpermonLocomotiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.dryport.LocomotiveDetailVO;
import com.screen.screendisplay.vo.dryport.LocomotiveStatusVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机车整体运行情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportOpermonLocomotiveServiceImpl extends ServiceImpl<DryportOpermonLocomotiveMapper, DryportOpermonLocomotive> implements DryportOpermonLocomotiveService {

    @Override
    public LocomotiveStatusVO getLocomotiveStatus(Integer year) {
        // 设置默认年份为2025
        if (year == null) {
            year = 2025;
        }
        
        // 查询指定年份的数据
        LambdaQueryWrapper<DryportOpermonLocomotive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DryportOpermonLocomotive::getYear, year);
        queryWrapper.orderByAsc(DryportOpermonLocomotive::getReceiveType);
        List<DryportOpermonLocomotive> entityList = this.list(queryWrapper);
        
        LocomotiveStatusVO response = new LocomotiveStatusVO();
        
        // 如果没有数据，返回空对象
        if (entityList == null || entityList.isEmpty()) {
            response.setDataList(new ArrayList<>());
            return response;
        }
        
        // 从第一条数据获取总设备量和占比信息（因为这些字段在每条记录中都是相同的）
        DryportOpermonLocomotive firstEntity = entityList.get(0);
        response.setTotalEquipment(firstEntity.getTotalEquipment());
        response.setCrc1Ratio(firstEntity.getCrc1Ratio());
        response.setCrc3Ratio(firstEntity.getCrc3Ratio());
        
        // 转换详细数据列表
        List<LocomotiveDetailVO> dataList = new ArrayList<>();
        for (DryportOpermonLocomotive entity : entityList) {
            LocomotiveDetailVO detailVO = new LocomotiveDetailVO();
            detailVO.setYear(entity.getYear());
            detailVO.setMonth(entity.getMonth());
            
            // 根据receive_type设置接车类型名称
            if (entity.getReceiveType() != null) {
                if (entity.getReceiveType() == 0) {
                    detailVO.setReceiveType("中铁一局");
                } else if (entity.getReceiveType() == 1) {
                    detailVO.setReceiveType("中铁三局");
                }
            }
            
            detailVO.setAssigned(entity.getAssigned());
            detailVO.setInUse(entity.getInUse());
            detailVO.setStandby(entity.getStandby());
            detailVO.setOverhaul(entity.getOverhaul());
            detailVO.setMediumRepair(entity.getMediumRepair());
            detailVO.setMinorRepair(entity.getMinorRepair());
            detailVO.setTemporaryRepair(entity.getTemporaryRepair());
            
            dataList.add(detailVO);
        }
        
        response.setDataList(dataList);
        
        return response;
    }

}
